package com.huanghuai.retail.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.BaseContext;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.entity.SInventory;
import com.huanghuai.retail.entity.Storage;
import com.huanghuai.retail.service.StorageService;
import com.huanghuai.retail.utils.VerifyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

/**
 * 对于仓库基本信息的增上改查
 */
@RestController
@RequestMapping("/storage")
public class StorageController
{

    @Resource
    StorageService storageService;



/*
* 添加一个新的仓库
* */
    @PostMapping("/add")
    public R<Object> addStorage(@RequestBody @Validated Storage storage, BindingResult bindingResult)
    {
        if (bindingResult.hasErrors())
        {
            return VerifyUtils.getValidateError(bindingResult);
        }
        boolean save = storageService.saveAndUpdate(storage);
        if (!save)
        {
            return R.error("业务繁忙,请稍后重试");
        }
        return R.success("success!");
    }

/*
* 对仓库基础信息的分页查询
* 默认展示五页十条数据
*
*
* */
    @GetMapping("/page")
    public R<Page> getPage(@RequestParam(value = "page",defaultValue = "5") int page,
                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                 @RequestParam(value = "name",required = false) String name,
                                 @RequestParam(value = "adminId",required = false ) Long adminId
                                 ){
        Page<Storage> storageInfo =new Page<>(page,pageSize);
//        按照条件查询
        LambdaQueryWrapper<Storage> queryWrapper=  new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Storage::getStorageName,name);
//        按照管理员  一个管理员可以管理多个仓库
        queryWrapper.eq(adminId!=null,Storage::getAdminId,adminId);
        List<Storage> list = storageService.list(queryWrapper);
        storageInfo.setRecords(list);
        return R.success(storageInfo);
    }

    /*
    * 对更新表单的数据回显
    * */
    @GetMapping("/change/{id}")
    public R<Storage> redisplay(@PathVariable("id") Long id)
    {
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Storage::getId,id);
        Storage storage = storageService.getOne(queryWrapper);
        if (storage==null)
        {
            return R.error("系统错误");
        }
        return R.success(storage);
    }

//    删除
    @DeleteMapping("/del/{id}")
    public R<Object> delStorage(@PathVariable Long id)
    {
        boolean b = storageService.removeStorageOne(id);
        if (!b)
        {
            return R.error("还有库存不能删除");
        }
        return R.success("删除成功") ;
    }

    /**
     * 查询所有仓库信息
     */
    @GetMapping("/findAllStorage")
    public R<List<Storage>> findAllStorage(){
        List<Storage> storages = storageService.list();
        if (storages == null || storages.size() == 0)
            return R.error("查询失败");
        return R.success(storages);
    }








}
